package edu.xcdq;

import java.util.Scanner;

public class BookManage {
    BookSet bookSet = new BookSet();
    Scanner sc = new Scanner(System.in);

    public void initial() {
        Book Javajichujiaocheng = new Book();
        Javajichujiaocheng.name = "Java基础教程";
        Javajichujiaocheng.state =0;
        Javajichujiaocheng.date = "2015 - 7 - 1";
        Book shujukujishu = new Book();
        shujukujishu.name = "数据库技术";
        shujukujishu.state = 1;
        shujukujishu.date = "0";
        Book renyueshenhua = new Book();
        renyueshenhua.name = "人月神话";
        renyueshenhua.state = 1;
        renyueshenhua.date = "0";
        bookSet.books[0] = Javajichujiaocheng;
        bookSet.books[1] = shujukujishu;
        bookSet.books[2] = renyueshenhua;
    }

    public void startMenu() {
        boolean flag = true;
        do {
            System.out.println("欢迎使用图书管理系统");
            System.out.println("-----------------------");
            System.out.println("1.新增图书");
            System.out.println("2.查看图书");
            System.out.println("3.删除图书");
            System.out.println("4.借出图书");
            System.out.println("5.归还图书");
            System.out.println("6.退出");
            System.out.println("-------------------------");
            System.out.println("请选择要执行的操作：");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("新增图书");
                    add();
                    break;
                case 2:
                    System.out.println("查看图书");
                    search();
                    break;
                case 3:
                    System.out.println("删除图书");
                    delete();
                    break;
                case 4:
                    System.out.println("借出图书");
                    sell();
                    break;
                case 5:
                    System.out.println("归还图书");
                    gui();
                    break;
                case 6:
                    System.out.println("谢谢使用！");
                    tuichu();
                    flag = false;
                    break;
                default:
                    System.out.println("输入不符合要求，请重新选择哦！");
                    break;
            }

        } while (flag);
    }

    public void search() {
        System.out.println("序号\t名称\t状态\t借出日期");
        for (int i = 0; i < bookSet.books.length; i++) {
            if (bookSet.books[i] != null) {
                bookSet.books[i].print(i + 1);
            }

        }
    }

    public void add() {
        System.out.println("请输入商品名称：");
        String name = sc.next();
        System.out.println("请输入状态：");
        int state = sc.nextInt();
        System.out.println("借出日期：");
        int date = sc.nextInt();
        Book book = new Book();
        book.name = name;
        book.state = state;
        //book.date  = date;
        for (int i = 0; i < bookSet.books.length; i++) {
            if (bookSet.books[i] == null) {
                bookSet.books[i] = book;
                break;
            }
        }

    }

    public void delete() {
        System.out.println("请输入书名序号：");
        boolean flag = true;
        int card = sc.nextInt();
        for (int i = 0; i < bookSet.books.length; i++) {
            if (bookSet.books[i] != null && (i + 1) == card) {
                int j = i;
                while (bookSet.books[j + 1] != null) {
                    bookSet.books[j] = bookSet.books[j + 1];
                    j++;
                }
                bookSet.books[j] = null;
                flag = true;
                break;
            } else {
                flag = false;
            }
        }
        if (flag) {
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败，请重新操作！");
        }

    }
    public void sell(){
        System.out.println("请输入你要借出的书名：");
        String name =sc.next();
        boolean flag =true;
        for (int i=0;i<bookSet.books.length;i++){
            if (bookSet.books[i]!=null&&bookSet.books[i].name.equals(name)){
                System.out.println("请输入这本书的状态：");
                int  state = sc.nextInt();
                if (state<=bookSet.books[i].state){
                    bookSet.books[i].state=bookSet.books[i].state+state;
                    bookSet.books[i].state=bookSet.books[i].state-state;
                    flag=true;
                }else {
                    System.out.println("这本书已经被借出，还没有归还！");
                    flag=false;
                }
                break;
            }else {
                flag=false;
            }
        }
        if (flag){
            System.out.println("成功借出！");
        }else {
            System.out.println("借出失败！");
        }
    }
     public  void gui(){
        System.out.println("请输入你要归还图书的名字：");
        String name = sc.next();
        System.out.println("归还图书的借出：");
       int i = sc.nextByte();
        System.out.println("归还图书的日期：");
         int shi = sc.nextByte();
         String y = sc.next();
         Book sc = new Book();
         sc.name = name;
         sc.date= y;
         sc.state= i;
         for (int j = 0 ;j < bookSet.books.length;j++){
             if (bookSet.books[j]==null){
          bookSet.books[j]=sc;
          break;
             }
               }
     }
     public  void tuichu(){
        System.out.println("您已成功退出！");
     }
}
